﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Internet_Cafe.App_Code.DatabaseModel;
using System.Data.Objects;

namespace Internet_Cafe.App_Code.DataBaseAccess
{
    public abstract class DbComputersAccess : DbAccess
    {
        /// <summary>
        /// Listanje na site Computers dodatno gi prikazuva koi se administratori a koi se workers
        /// </summary>
        /// <returns></returns>
        public static List<Computer> GetComputers()
        {
            using (computerCaffeDBEntities db = new computerCaffeDBEntities())
            {
                List<Computer> computerList = new List<Computer>();
                var obj = (from p in db.Computers select p);
                foreach (Computer u in obj)
                {
                    computerList.Add(u);
                }
                return computerList;
            }
        }


        /// <summary>
        /// Set computer positions doubles
        /// </summary>
        /// <param name="computerID"></param>
        /// <param name="posLeft"></param>
        /// <param name="posTop"></param>
        public static void SetComputerPosition(string computerID, double posLeft, double posTop)
        {
            using (computerCaffeDBEntities db = new computerCaffeDBEntities())
            {
                try
                {
                    Computer computer = (Computer)((from p in db.Computers where p.ID == computerID select p).First());
                    computer.PosLeft = posLeft;
                    computer.PosTop = posTop;
                    db.SaveChanges();
                }
                catch
                {
                }
            }
        }
        /// <summary>
        /// Set computer positions floats
        /// </summary>
        /// <param name="computerID"></param>
        /// <param name="posLeft"></param>
        /// <param name="posTop"></param>
        public static void SetComputerPosition(string computerID, float posLeft, float posTop)
        {
            using (computerCaffeDBEntities db = new computerCaffeDBEntities())
            {
                try
                {
                    Computer computer = (Computer)((from p in db.Computers where p.ID == computerID select p).First());
                    computer.PosLeft = (double)posLeft;
                    computer.PosTop = (double)posTop;
                    db.SaveChanges();
                }
                catch
                {
                }
            }
        }




        /// <summary>
        /// Brisi gi site kompjuteri
        /// </summary>
        /// <returns></returns>
        public static bool DeleteAll()
        {
            using (computerCaffeDBEntities db = new computerCaffeDBEntities())
            {
                try
                {
                    var obj = (from p in db.Computers select p);
                    db.DeleteObject(obj);
                    db.SaveChanges();
                    return true;
                }
                catch
                {
                }
                return false;
            }
        }

        /// <summary>
        /// Vnesuvanje na nov kompjuter
        /// </summary>
        /// <param name="computer"></param>
        /// <returns></returns>
        public static string Insert(Computer computer)
        {
            using (computerCaffeDBEntities db = new computerCaffeDBEntities())
            {
                try
                {
                    string newID = Guid.NewGuid().ToString();
                    computer.ID = newID;
                    db.AddToComputers(computer);
                    db.SaveChanges();

                    return computer.ID;
                }
                catch
                {
                }
                return null;
            }
        }


        /// <summary>
        /// Update na korisnik
        /// </summary>
        /// <param name="computerID"></param>
        /// <param name="computer"></param>
        /// <returns></returns>
        public static bool Update(string computerID, Computer computer)
        {
            using (computerCaffeDBEntities db = new computerCaffeDBEntities())
            {
                try
                {
                    Computer oldComputer = (Computer)((from p in db.Computers where p.ID == computerID select p).First());
                    oldComputer.Name = computer.Name;
                    oldComputer.IPAddress = computer.IPAddress;
                    oldComputer.PosLeft = computer.PosLeft;
                    oldComputer.PosTop = computer.PosTop;
                    db.SaveChanges();
                    return true;
                }
                catch
                {
                }
                return false;
            }
        }


        /// <summary>
        /// Brisenje na kompjuter
        /// </summary>
        /// <param name="computerID"></param>
        /// <returns></returns>
        public static bool Delete(string computerID)
        {
            using (computerCaffeDBEntities db = new computerCaffeDBEntities())
            {
                try
                {
                    Computer computer = (Computer)((from p in db.Computers where p.ID == computerID select p).First());
                    db.DeleteObject(computer);
                    db.SaveChanges();
                }
                catch
                {
                    return false;
                }
                return true;
            }
        }



        /// <summary>
        /// Zemi kompjuter od baza po ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Computer GetComputer(string computerID)
        {
            using (computerCaffeDBEntities db = new computerCaffeDBEntities())
            {
                try
                {
                    return (Computer)((from p in db.Computers where p.ID == computerID select p).First());
                }
                catch
                {
                }
                return null;
            }
        }
    }
}
